//
//  TrainingViewController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/19.
//

#import "TrainingViewController.h"
#import "OneTabButView.h"
#import "CoachsBookInputModel.h"
#import "CoachsBookTextFieldCell.h"
#import "LongPickerView.h"
#import "LongPickerViewModel.h"
@interface TrainingViewController ()<UITableViewDelegate,UITableViewDataSource,OneTabButViewDelegate,UITextFieldDelegate,LongPickerViewDelegate>
@property (nonatomic,strong) LongPickerView *myPickerView;
@end

@implementation TrainingViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navgationheadv.titlelabel.text = @"班型信息";
    
    OneTabButView *tabbutview = [[OneTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    tabbutview.delegate = self;
    [tabbutview.tabbut setTitle:@"保存" forState:UIControlStateNormal];
    [self.view addSubview:tabbutview];
    
    [self getData];
}

- (void)getData
{
    NSArray *titlearr = @[];
    NSArray *promptarr = @[];
    NSArray *celltypearr = @[];
    titlearr = @[@"训练场名称",@"训练场科目类型",@"训练场地址"];
    promptarr = @[@"请填写名称",@"请选择类型",@"请选择地址"];
    celltypearr = @[@"1",@"1",@"1"];
    for (NSInteger i=0; i<titlearr.count; i++) {
        CoachsBookInputModel *model = [[CoachsBookInputModel alloc]init];
        model.title = titlearr[i];
        model.prompt = promptarr[i];
        model.cellType = [celltypearr[i] integerValue];
        model.showArrow = NO;
        if (i==1||i==2) {
            model.showArrow = YES;
        }
        [self.dataArray addObject:model];
    }

}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-SafeAreaBottomHeight-110);
    
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"详细信息";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    
    self.tableView.tableHeaderView = HeaderV;
    
    NSString *promptStr = @"1、请仔细填写训练场信息，信息将在教练宝典面向所有学员展示。\n2、请核对训练场地址，学员会参考练车距离判断是否报名。\n3、请填写训练科目类型，学员会了解的更清楚。";
    CGFloat BJViewheight = 40 + [LongHelper textHeight:@"1、请仔细填写训练场信息，信息将在教练宝典面向所有学员展示。" WithUIFont:kPingFangRegular(12) AndWidth:APPwidth-60] + [LongHelper textHeight:@"2、请核对训练场地址，学员会参考练车距离判断是否报名。" WithUIFont:kPingFangRegular(12) AndWidth:APPwidth-60] + [LongHelper textHeight:@"3、请填写训练科目类型，学员会了解的更清楚。" WithUIFont:kPingFangRegular(12) AndWidth:APPwidth-60] + 10;
    
    UIView *FooterV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, BJViewheight+30)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    UIView *BJView = [[UIView alloc]initWithFrame:CGRectMake(15, 20, APPwidth-30, BJViewheight)];
    BJView.backgroundColor = rgba(245, 246, 249, 1);
    BJView.layer.cornerRadius = 8;
    BJView.layer.masksToBounds = YES;
    [FooterV addSubview:BJView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, APPwidth-60, 20)];
    titleLabel.text = @"提示:";
    titleLabel.textColor = rgba(51, 51, 51, 1);
    titleLabel.font = kPingFangRegular(14);
    [BJView addSubview:titleLabel];
    
    UILabel *promptLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(titleLabel.frame)+10, APPwidth-60, BJViewheight-50)];
    promptLabel.text = promptStr;
    promptLabel.textColor = rgba(102, 102, 102, 1);
    promptLabel.font = kPingFangRegular(12);
    promptLabel.numberOfLines = 0;
    [BJView addSubview:promptLabel];
    

    self.tableView.tableFooterView = FooterV;
    
    [self setMyPickerView];
}

#pragma mark - 设置自定义选择器
-(void)setMyPickerView
{
    self.myPickerView = [[LongPickerView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, APPheight) dataArr:@[]];
    [[UIApplication sharedApplication].keyWindow addSubview:self.myPickerView];
    self.myPickerView.delegate = self;
    self.myPickerView.hidden = YES;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CoachsBookInputModel *model = self.dataArray[indexPath.row];
    CoachsBookTextFieldCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextFieldCell"];
    if (!cell) {
        cell=[[CoachsBookTextFieldCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextFieldCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellFillWithModel:model];
    cell.contentField.tag = indexPath.row;
    cell.contentField.delegate = self;
    return cell;
    
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:( UITextField*)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (model.showArrow) {
        UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        textField.inputView = dummyView; // Hide keyboard,but show blinking cursor
        NSMutableArray *PickerViewModelArr = [[NSMutableArray alloc]init];
        [PickerViewModelArr removeAllObjects];
        if (textField.tag == 1) {
            self.myPickerView.mypickerView.tag = textField.tag;
            NSArray *titlearr = @[@"科目二",@"科目三",@"科目二和科目三"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 2)
        {
//            [self.view endEditing:YES];
        }
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (textField.text.length) {
        model.content = textField.text;
    }
}

#pragma mark - CYWKLPickerViewDelegate
-(void)choosPickerView:(UIPickerView *)pickerView WithRowStr:(NSString *)rowStr AndRowNum:(NSInteger)row
{
    CoachsBookInputModel *model = self.dataArray[1];
    model.content = rowStr;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = rowStr;
    [self.view endEditing:YES];
    
}

-(void)closePickerView:(UIPickerView *)pickerView
{
    [self.view endEditing:YES];
}


#pragma mark -OneTabButViewDelegate(保存)
- (void)OneTabButViewBtnSelectedDelegate:(NSInteger)tag
{
    
}

-(void)returnback{
    
    if (self.setTrainingBlock) {
        self.setTrainingBlock(@"dafdasfsa");
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
